package cn.lan.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreateEntityVoImpl implements CreateFile {

    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {
        String packageName = ConfigUtil.getVoPackage(projInfo);
        String entityPackName = ConfigUtil.getEntityPackage(projInfo);
        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_ENTITY_VO_PATH;

        String suffixPath = packageName.replaceAll("\\.", "\\\\") + "\\" + entityInfo.getEntityName() + "Vo" + Constant.FILE_SUFFIX_JAVA;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getService(),  Constant.PathType.basePath, suffixPath);

        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName",packageName);
                put("tableComment",entityInfo.getTableComment());
                put("projectAuthor",projInfo.getAuthor());
                put("upperEntityName", entityInfo.getEntityName());
                put("entityPackage",entityPackName);
                put("nowDate", DateUtil.today());
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }

}
